﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SzCourt.Logic.Common;
using KMFrame.Core;
using SzCourt.Logic.NBL;
using NBearLite;

namespace SzCourt.Logic.BLL
{
    public class Articles : SzCourtLogic
    {
        private ConditionCollection GetWorkReport(KeyWordCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                SZCourtColumns.Articles.ID,
                SZCourtColumns.Articles.nTitle, 
                SZCourtColumns.Articles.nDate
            };
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = SZCourtColumns.Articles.nBClass == condition.Type.nBClass;
            if (condition.KeyWord != null && condition.KeyWord.Length > 0)
                where = where && SZCourtColumns.Articles.nTitle.Contains(condition.KeyWord);
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(SZCourtColumns.Articles, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), SZCourtColumns.Articles.ID)
                .OrderBy(SZCourtColumns.Articles.nDate.Desc);

            return cdtColl;
        }

        public PageResult GetWorkReportPage(KeyWordCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetWorkReport(condition).Select.ToList<WorkReportItem>();
            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.Articles, SZCourtColumns.Articles.ID, GetWorkReport(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetArticles(KeyWordCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                SZCourtColumns.Articles.ID,
                SZCourtColumns.Articles.nTitle, 
                SZCourtColumns.Articles.nDate
            };
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = WhereClip.All;
            if (condition.Type.nBClass > -1)
                where = where && SZCourtColumns.Articles.nBClass == condition.Type.nBClass;
            if (condition.Type.nBoard > -1)
                where = where && SZCourtColumns.Articles.nBoard == condition.Type.nBoard;
            if (condition.KeyWord != null && condition.KeyWord.Length > 0)
                where = where && SZCourtColumns.Articles.nTitle.Contains(condition.KeyWord);
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(SZCourtColumns.Articles, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), SZCourtColumns.Articles.ID)
                .OrderBy(SZCourtColumns.Articles.nDate.Desc);

            return cdtColl;
        }

        public PageResult GetArticlesPage(KeyWordCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetArticles(condition).Select.ToList<WorkReportItem>();
            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.Articles, SZCourtColumns.Articles.ID, GetWorkReport(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        public SzCourt.Logic.NBL.DBEntities.Articles GetArticlesInfo(SzCourt.Logic.NBL.DBEntities.Articles condition)
        {
            return NBLData
               .Select(SZCourtColumns.Articles)
               .Where(SZCourtColumns.Articles.ID == condition.ID)
               .ToSingleObject<SzCourt.Logic.NBL.DBEntities.Articles>();
        }

        public IEnumerable<WorkReportItem> GetArticlesTop(ArticlesType type)
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                SZCourtColumns.Articles.ID,
                SZCourtColumns.Articles.nTitle, 
                SZCourtColumns.Articles.nDate
            };
            WhereClip where = WhereClip.All;
            if (type.nBClass > -1)
                where = where && SZCourtColumns.Articles.nBClass == type.nBClass;
            if (type.nBoard > -1)
                where = where && SZCourtColumns.Articles.nBoard == type.nBoard;
            return NBLData.Select(SZCourtColumns.Articles, selectColumns)
                .Where(where)
                .SetSelectRange(5, 0, SZCourtColumns.Articles.ID)
                .OrderBy(SZCourtColumns.Articles.ID.Desc)
                .ToList<WorkReportItem>();
        }

        public bool AddArticles(SzCourt.Logic.NBL.DBEntities.Articles param)
        {

            int ri = NBLData.Insert(SZCourtColumns.Articles)
                .AddColumn(SZCourtColumns.Articles.nInfo, param.nInfo)
               
                .ExecuteReturnAutoIncrementID(SZCourtColumns.pIdeasBox.IdeasBoxID);
            return true;
        }
    }

    public class WorkReportItem
    {
        public int ID { get; set; }
        public string nTitle { get; set; }
        public DateTime nDate { get; set; }
    }

    public class KeyWordCondition : PageParam
    {
        public string KeyWord { get; set; }
        public ArticlesType Type { get; set; }
    }

    public class ArticlesType
    {
        public int nBClass { get; set; }
        public int nBoard { get; set; }
        public ArticlesType(int nbclass, int nboard)
        {
            this.nBClass = nbclass;
            this.nBoard = nboard;
        }
    }

    public static class ArticlesTypes
    {
        /// <summary>
        /// 送达公告
        /// </summary>
        public static ArticlesType SDGG
        {
            get
            {
                return new ArticlesType(79, 36);
            }
        }

        /// <summary>
        /// 评估拍卖机构名单
        /// </summary>
        public static ArticlesType PGJGMD
        {
            get
            {
                return new ArticlesType(79, 37);
            }
        }

        /// <summary>
        /// 中介机构公告
        /// </summary>
        public static ArticlesType ZJJGGG
        {
            get
            {
                return new ArticlesType(79, 38);
            }
        }

        /// <summary>
        /// 督促履行债务令公告
        /// </summary>
        public static ArticlesType DCLXZWLGG
        {
            get
            {
                return new ArticlesType(79, 39);
            }
        }

        /// <summary>
        /// 支付令
        /// </summary>
        public static ArticlesType ZFL
        {
            get
            {
                return new ArticlesType(79, 40);
            }
        }

        /// <summary>
        /// 破产公告
        /// </summary>
        public static ArticlesType PCGG
        {
            get
            {
                return new ArticlesType(79, 41);
            }
        }

        /// <summary>
        /// 其他公告
        /// </summary>
        public static ArticlesType QTGG
        {
            get
            {
                return new ArticlesType(79, 43);
            }
        }

        /// <summary>
        /// 执行法律法规规章
        /// </summary>
        public static ArticlesType ZXFLGZ
        {
            get
            {
                return new ArticlesType(188, -1);
            }
        }

        /// <summary>
        /// 执行工作动态
        /// </summary>
        public static ArticlesType ZXGZDT
        {
            get
            {
                return new ArticlesType(189, -1);
            }
        }

        /// <summary>
        /// 执行故事
        /// </summary>
        public static ArticlesType ZXGS
        {
            get
            {
                return new ArticlesType(190, -1);
            }
        }

        /// <summary>
        /// 执行公告
        /// </summary>
        public static ArticlesType ZZGG
        {
            get
            {
                return new ArticlesType(200, -1);
            }
        }

        /// <summary>
        /// 中院报告
        /// </summary>
        public static ArticlesType ZYBG
        {
            get
            {
                return new ArticlesType(82, -1);
            }
        }

        /// <summary>
        /// 审批工作白皮书
        /// </summary>
        public static ArticlesType SPGZ
        {
            get
            {
                return new ArticlesType(199, -1);
            }
        }
    }
}
